Skip to content

Fix ImageGeneratingChatClient duplicating preceding content and dropping following content#7622

Merged
jozkee merged 3 commits into
mainfrom
copilot/fix-image-generating-chatclient-duplicates
Jul 13, 2026
Merged

Fix ImageGeneratingChatClient duplicating preceding content and dropping following content#7622
jozkee merged 3 commits into
mainfrom
copilot/fix-image-generating-chatclient-duplicates

Conversation

Copilot AI commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

ReplaceImageGenerationFunctionResults iterated in reverse, causing content items after an image-generation match to be silently dropped (visited while newContents was null) and items before it to be duplicated (CopyList captured the prefix, then the reverse loop re-appended those same items).

For a message with contents [A, ImageGenFunctionCall, B], the old code produced [A, ImageGenerationToolCallContent, A] — dropping B and duplicating A.

Changes

  • ImageGeneratingChatClient.cs: Changed ReplaceImageGenerationFunctionResults from reverse to forward iteration. CopyList(contents, i) now correctly captures only items before the replacement point; remaining items are appended naturally as the loop continues.

  • ImageGeneratingChatClientTests.cs: Added regression tests for both GetResponseAsync and GetStreamingResponseAsync covering image-generation content at the start, middle, and end of the content list with surrounding unrelated content items of mixed types (FunctionResultContent, UsageContent).

Copilot AI changed the title [WIP] Fix duplication and content drop in ImageGeneratingChatClient Fix ImageGeneratingChatClient duplicating preceding content and dropping following content Jul 13, 2026
Copilot AI requested a review from jozkee July 13, 2026 15:18
@jozkee jozkee requested a review from Copilot July 13, 2026 15:44

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a content-rewrite bug in ImageGeneratingChatClient where reverse iteration could duplicate earlier content items and drop later items when replacing image-generation function content.

Changes:

  • Updated ReplaceImageGenerationFunctionResults to iterate forward so surrounding content is preserved and not duplicated/dropped.
  • Added regression tests for both non-streaming and streaming responses to validate content order and cardinality when image-generation content appears at the start/middle/end.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Libraries/Microsoft.Extensions.AI/ChatCompletion/ImageGeneratingChatClient.cs Switches replacement logic to forward iteration to preserve content order and prevent duplication/dropping.
test/Libraries/Microsoft.Extensions.AI.Tests/ChatCompletion/ImageGeneratingChatClientTests.cs Adds regression coverage for surrounding-content preservation in both GetResponseAsync and GetStreamingResponseAsync.

Comment thread src/Libraries/Microsoft.Extensions.AI/ChatCompletion/ImageGeneratingChatClient.cs Outdated
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@jozkee jozkee marked this pull request as ready for review July 13, 2026 19:41
@jozkee jozkee requested a review from a team as a code owner July 13, 2026 19:41
@jozkee jozkee enabled auto-merge (squash) July 13, 2026 19:45
@jozkee jozkee merged commit be18e99 into main Jul 13, 2026
5 checks passed
@jeffhandley

Copy link
Copy Markdown
Member

/backport to release/10.8

@github-actions

Copy link
Copy Markdown
Contributor

Started backporting to release/10.8: https://github.com/dotnet/extensions/actions/runs/29286341323

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Microsoft.Extensions.AI] ImageGeneratingChatClient duplicates preceding contents and drops following contents

4 participants